package cn.gz.sboot31.controller;

import cn.gz.sboot31.entity.Emp;
import cn.gz.sboot31.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmpController {

    //此注解会创建一个EmpMapper的实现类。并实例化出一个对象，自动装配给empMapper
    @Autowired
    private EmpMapper empMapper;

    @RequestMapping("/add")
    public String add(Emp emp){
        empMapper.insert(emp);
        return "添加成功";
    }

    @RequestMapping("/select")
    public String select(){
        List<Emp> emps = empMapper.select();

        String html = "<table border='1'>";
        html+="<caption>员工列表</caption>";
        html+="<tr><td>id</td><td>name</td><td>job</td><td>sal</td><td>操作</td></tr>";
        for(Emp e:emps){
            html+="<tr><td>"+e.getId()+"</td><td>"+e.getName()+"</td><td>"+e.getJob()+"</td><td>"+e.getSal()+"</td><td><a href='/delete?id="+e.getId()+"'>删除</a></td></tr>";
        }
        html+="</table>";
        return html;
    }

    @RequestMapping("/delete")
    public String delete(int id){
        empMapper.delete(id);
        return "删除成功，<a href='/select'>返回员工列表<a>";
    }

    @RequestMapping("/update")
    public String update(Emp emp){
        empMapper.update(emp);
        return "修改成功，<a href='/select'>返回员工列表<a>";
    }
}
